#ifndef _SPHY_H_
#define _SPHY_H_

#include "PocoHeaders.h"
#include "../SerialChannel.h"
#include "../SerialModule.h"

//<s,w,len,n,slleUUID,total,index,scsfUUID,method,syycUUID,data,md5,w,s>

#define MAX_SPHY_LENGTH       (MAX_SLLE_LENGTH + 100)
#define MAX_SPHY_READ_LENGTH  2048

class SPHY : public Poco::Runnable
{
public:
    static SPHY* Initialize();
    static SPHY* GetInstance();

public:
    int Start();
    int Stop();

public:
    int Write(std::string strSLLEData);
    std::string Read();

private:
    SPHY();
    ~SPHY();
    virtual void run();
    void Routine();

private:
    SerialChannel* GetSerialChannel(std::string strName);

private:
    std::string MakeMd5(std::string strData);
    std::string CompileWriteData(std::string strData, std::string strMd5, int iTimes);
    std::string CompileRecvData(std::string strData, std::string strMd5, int iTimes);
    bool HasReadMd5(std::string strMd5);
    void DealSerialData(std::string strData);
    bool CheckData(std::string strData);
    int SaveData(std::string strData);
    int GetFrameLen(std::string m_strData);
    int GetLenLen(int iFrame);

private:
    int SerialWrite(std::string strData);

private:
    static SPHY*   m_pInstance;

private:
    Poco::Thread   m_thread;
    bool           m_bStop;
    Poco::Event    m_ready;

private:
    SerialChannel* m_pSerialChn;
    Poco::Mutex    m_mutexSerialWrite;

private:
    Poco::Mutex              m_mutexRecvData;
    std::vector<std::string> m_vectRecvData;
    std::string              m_strRecvData;
    std::string              m_strMd5;
};
#endif // !_SPHY_H_
